import cv2
import os

def video2img(path, out_path,filename):
    cam1 = cv2.VideoCapture(path)
    # 获取视频的帧频
    CAMERA_FPS = cam1.get(cv2.CAP_PROP_FPS)  # 25帧/秒
    print('帧频:' + str(CAMERA_FPS))
    # 获取视频的帧数
    CAP_PROP_FRAME_COUNT = cam1.get(cv2.CAP_PROP_FRAME_COUNT)  # 25帧/秒
    print("帧数：" + str(CAP_PROP_FRAME_COUNT))
    # 定义视频编码格式
    cv2.VideoWriter_fourcc(*'mp4v')
    num = 0
    while True:
        # 读取视频
        (ok1, frame1) = cam1.read()
        # 视频最后一秒不参与合并到新视频
        if ok1:
            frame1 = cv2.resize(frame1, (640, 640))
            filepathnew = os.path.join(out_path, filename + str(num) + '.jpg')
            cv2.imwrite(filepathnew, frame1)
            num += 1
        else:
            print(num)
            break
    cam1.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    path = r'C:\Users\long\Desktop\111\CH1020510521122.mp4'
    filename = 'CH1020510521122.mp4'
    out_path = r'H:\vtoimg'
    video2img(path, out_path,filename)
